package org.lixue.common.util;


import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * Created by lixue on 13:41 2018/8/15
 */
@Slf4j
public class FileUtil {
    public FileUtil() {
    }

    public static File getFile(String fileName) {
        return getFile(fileName, false);
    }

    public static File getFile(String fileName, boolean mkdir) {
        File file = new File(fileName);
        if (mkdir && !file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }

        return file;
    }

    public static boolean mkDirs(String dir) {
        boolean bRet = false;

        try {
            File file = getFile(dir);
            if (!file.exists() && file.mkdirs()) {
                bRet = true;
            }
        } catch (Exception var3) {
            log.error("创建目录出错", var3);
        }

        return bRet;
    }

    public static void writIn(InputStream in, String fileName) throws IOException {
        DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName));
        byte[] buffer = new byte[4096];
        boolean var4 = false;

        int count;
        while ((count = in.read(buffer)) > 0) {
            out.write(buffer, 0, count);
        }

        out.close();
        in.close();
    }

    public static void writIn(String fileName, String str, String encoding) {
        OutputStreamWriter ow = null;
        BufferedWriter bw = null;

        try {
            File newFile = getFile(fileName, true);
            ow = new OutputStreamWriter(new FileOutputStream(newFile), encoding);
            bw = new BufferedWriter(ow);
            bw.write(str);
            bw.flush();
        } catch (IOException var14) {
            log.error("写文件出错", var14);
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }

                if (ow != null) {
                    ow.close();
                }
            } catch (Exception var13) {
                ;
            }

        }

    }

    public static void writIn(String fileName, String str) {
        writIn(fileName, str, "UTF-8");
    }

}
